//
//  LDRefreshControl.swift
//  Weibo201655
//
//  Created by pro on 16/5/12.
//  Copyright © 2016年 LeeDan. All rights reserved.
//

import UIKit

class LDRefreshControl: UIRefreshControl {

    override init() {
        super.init()
        
        setupUI()
    }
//    override init(frame: CGRect) {
//        super.init(frame: frame)
//        
//        setupUI()
//    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupUI() {
        addSubview(refreshView)
        
        refreshView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(self.frame.size)
            make.centerX.centerY.equalTo(self)
        }
    }
    //MARK:----------------------- 系统属性和方法 ----------------------------------//
    override var frame: CGRect {
        didSet {
            //print(frame)
            
            if frame.origin.y > 0 {
                return
            }
            
            // 能到下面来y为负数
            // 0 -  -60箭头向下
            // 小于-60,箭头向上
            if frame.origin.y <= -60 {
                refreshView.animationWithArrowView(true)
            } else {
                refreshView.animationWithArrowView(false)
            }
            
            //判断是否进入刷新状态
            if refreshing {
                refreshView.loadingAnimation()
            }
        }
    }
    /**
     当tableView回到原来的位置时,结束刷新
     */
    override func endRefreshing() {
         super.endRefreshing()
        
         refreshView.stopLoadingAni()
    }
    
    //MARK:----------------------- 懒加载 ----------------------------------//
    private lazy var refreshView: LDRefreshView = LDRefreshView.refreshView()
    
    
}
